/******************************************************************************
@file:  GeekHomeScreen.java
@author Ramesh Anandhan

DESCRIPTION
   Geekwave home screen.
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
 ******************************************************************************/

package com.smartc.ui;

import java.util.ArrayList;

import com.smartc.foodmenu.FoodMenuHome;
import com.smartc.health.HealthTabHome;
import com.smartc.notification.Notification;
import com.smartc.soffer.SpecialOffer;
import com.smartc.trading.TradeHome;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/*
 * Home screen
 */
public class GeekHomeScreen extends Activity implements OnItemClickListener {
	private final String TAG = "GeekHomeScreen";
	private final int TRANSPORT = 0x00;
	private final int FOODMENU = 0x01;
	private final int HEALTHCARE = 0x02;
	private final int SOFFER = 0x03;
	private final int TRADING = 0x04;
	private final int NOTIFICATION = 0x05;
	private GridView mGrid;
	private ArrayList<Appinfo> mApps = new ArrayList<Appinfo>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.home);
		loadApps();
		mGrid = (GridView) findViewById(R.id.myGrid);
		mGrid.setAdapter(new AppsAdapter(getApplicationContext()));
		mGrid.setOnItemClickListener(this);
	}

	/*
	 * class hold all application infos
	 */
	public class Appinfo {
		public String mAppName;
		public Drawable mAppImage;

		Appinfo(int stringID, int imageID) {
			mAppName = getResources().getString(stringID);
			mAppImage = getResources().getDrawable(imageID);
		}

		Appinfo() {
		}
	}

	/*
	 * Adapter represents the data to be displayed by grid view
	 */
	public class AppsAdapter extends BaseAdapter {
		private final LayoutInflater mInflater;

		public AppsAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.application_box,
						parent, false);
			}
			TextView textView = (TextView) convertView;
			textView.setTextColor(Color.BLACK);
			Appinfo info = mApps.get(position);
			textView.setText(info.mAppName);
			textView.setCompoundDrawablesWithIntrinsicBounds(null,
					info.mAppImage, null, null);
			return convertView;
		}

		public final int getCount() {
			return mApps.size();
		}

		public final Object getItem(int position) {
			return mApps.get(position);
		}

		public final long getItemId(int position) {
			return position;
		}
	}

	/*
	 * input all application details
	 */
	private void loadApps() {
		Appinfo app_transport = new Appinfo(R.string.app_transport,
				R.drawable.travel_bus);
		Appinfo app_tastit = new Appinfo(R.string.app_foodmenu,
				R.drawable.food_menu);
		Appinfo app_gohealthy = new Appinfo(R.string.app_healthcare,
				R.drawable.gohealthy);
		Appinfo app_soffers = new Appinfo(R.string.app_soffers,
				R.drawable.specialoffer);
		Appinfo app_tradepoint = new Appinfo(R.string.app_trading,
				R.drawable.tradepoint);
		Appinfo app_notifications = new Appinfo(R.string.app_notifications,
				R.drawable.notification);

		mApps.add(app_transport);
		mApps.add(app_tastit);
		mApps.add(app_gohealthy);
		mApps.add(app_soffers);
		mApps.add(app_tradepoint);
		mApps.add(app_notifications);
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		if (AccountLogin.DEBUG)
			Log.i(TAG, "position:" + position);
		Intent intent = new Intent();
		switch (position) {
		case TRANSPORT:
			// intent.setClass(this, TransportHome.class);
			intent.setClass(this, TransportTabHome.class);
			startActivity(intent);
			break;
		case TRADING:
			intent.setClass(this, TradeHome.class);
			startActivity(intent);
			break;
		case FOODMENU:
			intent.setClass(this, FoodMenuHome.class);
			startActivity(intent);
			break;
		case HEALTHCARE:
			//Intent intent1 = new Intent("com.smartc.health.HealthTabHome");
			intent.setClass(this, HealthTabHome.class);
			startActivity(intent);
			break;
		case SOFFER:
			intent.setClass(this, SpecialOffer.class);
			startActivity(intent);
			break;
		case NOTIFICATION:
			intent.setClass(this, Notification.class);
			startActivity(intent);
			break;
		}

		
	}

}
